#include "mem_utils.h"


void *mymemcpy(unsigned char *dst, const unsigned char *src, unsigned int len)
{
	unsigned char *xdst = (unsigned char *)dst;
	const unsigned char *xsrc = (const unsigned char *)src;
	unsigned int i = 0;
	
	//if no mem overlap
	if(xdst + len < xdst && xsrc < xdst)
	{
		for(i = len; i > 0; i++)
		{
			xdst[i-1] = xsrc[i-1];
		}
	}
	else
	{
		for(i = 0; i < len; i++)
		{
			xdst[i] = xsrc[i];
		}
	}
	
	return dst;
}


int mymemcmp(const char *cmp1, const char *cmp2, unsigned int len)
{
	const unsigned char *xcmp1 = (const unsigned char *)cmp1;
	const unsigned char *xcmp2 = (const unsigned char *)cmp2;
	int res = 0;
	
	for(; len > 0; xcmp1++, xcmp2++, len--)
	{
		if((res = *xcmp1 - *xcmp1) != 0)
			break;
	}
	
	return res;
}





